import django.http as http
import django.shortcuts as shortcuts
from django.template import RequestContext
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.shortcuts import get_object_or_404
from django.core.urlresolvers import reverse

from core.forms import AddRelatedForm 
from comments.forms import CommentForm
from forms import AnswerForm
import community.helpers as community_helpers
import forms
import models
import django_widgets.pagination as pagination
import django_widgets.request_helpers as request_helpers
import community.decorators as community_decorators
from django_reputation.decorators import reputation_required

@community_decorators.user_required
def accept_answer(request,  answer_id):
    answer = models.Answer.objects.get(id = answer_id)
    question = answer.question
    if not question.has_accepted():
        answer.accept()
    redirect_url = reverse('qa-view-question',  args=[question.id])
    return http.HttpResponseRedirect(redirect_url)
    
def qa(request,  option = 'most_active'):
    page = request_helpers.get_page(request)
    questions = models.Question.get_sorted_objects(models.Question,  option)
    
    paginator = pagination.DiggPaginator(questions, 10, body=5)
    current_page = paginator.page(page)
    page_range = paginator.page_range
    object_count = len(current_page.object_list)
    
    help_template = 'qa/elements/help.html'
    return shortcuts.render_to_response(
                'qa/questions.html', 
                {'current_page':current_page,  'page_range':page_range, 'help_template':help_template,  'sort':option}, 
                context_instance = RequestContext(request),
    )

@community_decorators.user_required
def ask(request):
    if request.POST:
        form = forms.QuestionForm(request.POST,  request.FILES,  request)
        if form.is_valid():
            form.handle_add(request)
    else:
        form = forms.QuestionForm()
    return shortcuts.render_to_response(
                'qa/ask.html', 
                {'form':form}, 
                context_instance = RequestContext(request),
    )

def view(request,  slug):
    from tracking.models import Views
    from django_widgets.request_helpers import get_ip
    from community.helpers import get_logged_user
    
    user = get_logged_user(request)
    question = get_object_or_404(models.Question, slug = slug)
    question.tags_with_count = question.get_tags_with_count()
    question.set_answers()
    answer_form = AnswerForm()
    
    Views.add_view(question,  get_ip(request))
    return shortcuts.render_to_response(
                'qa/view.html', 
                {'question':question,  'node':question,  'answer_form':answer_form,  'is_owner':question.is_owner(user),  'has_accepted':question.has_accepted()}, 
                context_instance = RequestContext(request),
    )

def view_question(request,  question_id):
    from community.helpers import get_logged_user
    
    user = get_logged_user(request)
    question = get_object_or_404(models.Question, id = question_id)
    question.tags_with_count = question.get_tags_with_count()
    question.set_answers()
    answer_form = AnswerForm()
    return shortcuts.render_to_response(
                'qa/view.html', 
                {'question':question,  'node':question,  'answer_form':answer_form,  'is_owner':question.is_owner(user),  'has_accepted':question.has_accepted()}, 
                context_instance = RequestContext(request),
    )
    
@community_decorators.user_required
@reputation_required('edit_content')
def edit(request,  slug):
    user = community_helpers.get_logged_user(request)
    
    question = get_object_or_404(models.Question, slug = slug)
    question.tags_with_count = question.get_tags_with_count()
    EditQuestionForm = forms.build_edit_question_form(question)
    if request.POST:
        form = EditQuestionForm(request.POST,  request.FILES)
        if form.is_valid():
            form.handle_edit(user,  question)
            return http.HttpResponseRedirect(reverse('qa-view',  args=[question.slug]))
    else:
        form = EditQuestionForm()
    return shortcuts.render_to_response(
                'qa/edit.html', 
                {'question':question,  'node':question,  'form':form}, 
                context_instance = RequestContext(request),
    )

@community_decorators.user_required
@reputation_required('edit_content')
def edit_answer(request,  answer_id):
    user = community_helpers.get_logged_user(request)
    
    answer = get_object_or_404(models.Answer,  id = answer_id)
    EditAnswerForm = forms.build_edit_answer_form(answer)
    if request.POST:
        form = EditAnswerForm(request.POST,  request.FILES)
        if form.is_valid():
            form.handle_edit(user,  answer)
            return http.HttpResponseRedirect(reverse('qa-view-answer',  args=[answer.id]))
    else:
        form = EditAnswerForm()
    return shortcuts.render_to_response(
        'qa/edit_answer.html', 
        {'answer':answer,  'node':answer, 'form':form}, 
        context_instance = RequestContext(request), 
    )

def view_answer(request,  answer_id):
    answer = get_object_or_404(models.Answer,  id = answer_id)
    question = answer.question
    answer_url = "%s#answer_%s" % (reverse('qa-view',  args=[question.slug]), str(answer.id))
    return http.HttpResponseRedirect(answer_url)

def answer(request,  slug):
    user = community_helpers.get_logged_user(request)

    if user and request.POST:
        answer_form = AnswerForm(request.POST,  request.FILES)
        question = get_object_or_404(models.Question, slug = slug)
        question.set_answers()
        answer_form.handle_add(request,  question)
    return shortcuts.render_to_response(
            'qa/answers.html', 
            {'question':question, 'node':question}, 
            context_instance = RequestContext(request), 
    )
